package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.ad.Coupons;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICouponsService;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 * 优惠券管理
 * @author TT
 *
 */
@Controller
@RequestMapping("Coupons")
public class CouponsController {
	
	@Autowired
	private ICouponsService couponsService;
	@Autowired
	private ICourseInfoService  courseInfoService;
	@Autowired
	private ICourseChapterService chapterService;
	@Autowired
	private IVipCardService cardService;

	
	/**
	 * 加载优惠券列表页面
	 * @param pageNo
	 * @param pageSize
	 * @param title
	 * @param ftype
	 * @param limits
	 * @param model
	 * @return
	 */
	@LogInfo("加载优惠券列表页面")
	@RequestMapping("CouponsList")
	public String CouponsList(@RequestParam(value="pageNo",required=false) Integer pageNo,
							  @RequestParam(value="pageSize",required=false) Integer pageSize,
							  @RequestParam(value="title",required=false) String title,
							  @RequestParam(value="ftype",required=false) String ftype,
							  @RequestParam(value="limits",required=false) String limits,
							  Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		map.put("ftype", ftype);
		map.put("limits", limits);
		
		PageBean<Coupons> pageBean = new PageBean<Coupons>(pageNo,pageSize,null,map);
		pageBean.setUrl("Coupons/CouponsList");
		couponsService.getCouponsList(pageBean);
		model.addAttribute("page",pageBean);
		return "ad/couponsList";
	}
	
	/**
	 * 添加/修改 代金券信息页面加载
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("添加/修改 代金券信息页面加载")
	@RequestMapping("CouponsLoad/{id}")
	public String CouponsLoad(@PathVariable("id") Integer id,Model model){
		if (id != null && id != 0) {
			Coupons coupons = this.couponsService.getCoupons(id);
			model.addAttribute("type","upd");
			model.addAttribute("coupons",coupons);
		}else {
			model.addAttribute("type","add");
		}
		
		List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 
		 List<CourseChapter> chapters = chapterService.queryCourseChapters();
		 model.addAttribute("chapters",chapters);
		 
		 List<VipCard> cards = cardService.queryVipCards();
		 model.addAttribute("cards",cards);
		
		return "ad/couponsEdit";
	}
	
	/**
	 * 添加/修改 代金券信息
	 * @param coupons
	 * @param request
	 * @return
	 */
	@LogInfo("添加/修改 代金券信息")
	@RequestMapping("CouponsEdit")
	public String CouponsEdit(@ModelAttribute Coupons coupons,HttpServletRequest request){
		if (coupons.getId()==null) {
			couponsService.saveCoupons(coupons);
		}else {
			couponsService.updateCoupons(coupons);
		}
		
		return "redirect:/Coupons/CouponsList";
	}
	
	/**
	 * 根据Id删除一条优惠券信息
	 * @param id
	 * @return
	 */
	@LogInfo("删除一条优惠券信息")
	@RequestMapping("CouponsDel/{id}")
	public String CouponsDel(@PathVariable("id") Integer id){
		couponsService.deleteCoupons(id);
		return "redirect:/Coupons/CouponsList";
	}
}
